﻿using Newtonsoft.Json;
using NLog.LayoutRenderers;
using Stee.CAP8.MessageBus.Interface.Models;
using Stee.CAP8.MessageBus.Sagas.Core;
using Stee.CAP8.MessageBus.Sagas.Core.Messages;
using Stee.CAP8.MessageBus.Sagas.Order.Sagas;
using Stee.CAP8.MessageBus.Sagas.Order.Sagas.Data;
using Stee.CAP8.MessageBus.Sagas.Payment.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stee.CAP8.MessageBus.Sagas.Order.Services
{
    public class OrderService : IOrderService
    {
        private IBus _bus;
        private OrderSagaMain _orderSagaMain;

        public OrderService(IBus bus, OrderSagaMain orderSagaMain)
        {
            _bus = bus ?? throw new ArgumentNullException(nameof(bus));
            _orderSagaMain = orderSagaMain ?? throw new ArgumentNullException(nameof(orderSagaMain));
        }

        public async Task<bool> CreateOrder(List<ProductInfo> productInfos)
        {
            //获取用户信息 token to userinfo
            var userId = Guid.NewGuid();
            var userName = "Saga测试员";
            var mobilePhone = "13333336666";
            var orderId = Guid.NewGuid();
            var orderNumber = "TMP15987412";
            OrderInfo orderInfo = new OrderInfo
            {
                UserId = userId,
                UserName = userName,
                Balance = productInfos.Sum(x => x.TotalPrice),
                MobilePhone = mobilePhone,
                OrderId = orderId,
                OrderNo = orderNumber
            };
          
            var sagaId = await _orderSagaMain.StartSaga(orderInfo);
            return true;
        }

        public async Task<bool> HandleOrderComplete(CommandMessage message) 
        {
            Console.WriteLine("订单创建完成或者失败");
            
            return await Task.FromResult(true);
        }
    }





}
